'libtgfx - Threaded Graphics library for FreeBASIC
'Copyright (C) 2013  Ebben Feagan <sir@mud.owlbox.net>
'
'This library is free software; you can redistribute it and/or
'modify it under the terms of the GNU Lesser General Public
'License as published by the Free Software Foundation; either
'version 2.1 of the License, or (at your option) any later version.
'
'This library is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
'Lesser General Public License for more details.
'
'You should have received a copy of the GNU Lesser General Public
'License along with this library; if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include once "tgfx.bi"

sub draw_thread2( byval cc__ as any ptr )
    var cc = cast(CommChannel ptr,cc__)

    var ret = cc->recv()
    while ret = 0
        tgfx.drawing.print(,"Hello, World!",tgfx.Coord(0,0))
    ret = cc->recv()
    wend
    delete ret
end sub

sub draw_thread( byval cc__ as any ptr )
    var cc = cast(CommChannel ptr,cc__)

    var ret = cc->recv()
    var x1 = 100
    var y1 = 100
    var x2 = 200
    var y2 = 200
    while ret = 0
        tgfx.window.clear()
        tgfx.drawing.line(,tgfx.Coord(x1,y1),tgfx.Coord(x2,y2))
        tgfx.drawing.circle(,tgfx.Coord(x1,y1),25,rgb(255,0,0),tgfx.true)
        x1 -= 1
        x2 -= 1
        if x1 < 0 then x1 = 320
        if x2 < 0 then x2 = 320
        sleep 10,1
    ret = cc->recv()
    wend
    delete ret
end sub

    var cc = new CommChannel()
    var cc2 = new CommChannel()
    dim dtid(0 to 1) as any ptr
    if tgfx.window.init(320,240) then
        tgfx.window.setTitle( "It Worked!" )
        dtid(0) = threadcreate (@draw_thread, cc)
        dtid(1) = threadcreate (@draw_thread2, cc2)
    else
        print "Oops, something went wrong intializing the screen"
    end if
    sleep
    cc->send(new Message(tgfx._MEXIT))
    cc2->send(new Message(tgfx._MEXIT))
    for n as integer = 0 to ubound(dtid)
        threadwait dtid(n)
    next
    delete cc
    delete cc2

